package cn.tedu.medicalpath.employee.service.impl;

import cn.tedu.medicalpath.employee.mapper.AuthorityListMapper;
import cn.tedu.medicalpath.employee.pojo.vo.AuthorityArrVO;
import cn.tedu.medicalpath.employee.pojo.vo.AuthorityListVO;
import cn.tedu.medicalpath.employee.service.AuthorityListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AuthorityListServiceImpl implements AuthorityListService {

    @Autowired
    private AuthorityListMapper authorityListMapper;

    @Override
    public Map<String, AuthorityArrVO[]> selectByRoleId(Integer roleId,String name) {
        log.debug("开始处理【查询角色权限列表】的业务，参数：{}", roleId);
        List<AuthorityListVO> list = authorityListMapper.selectByRoleId(roleId);
        int index = 0;
        for (AuthorityListVO authorityListVO : list){
            if(authorityListVO.getModuleId() == null){
                index++;
            }
        }
        AuthorityArrVO[] authorityArr = new AuthorityArrVO[index];
        int count = 0;
        for (AuthorityListVO authorityListVO : list) {
            AuthorityArrVO authorityArrVO = new AuthorityArrVO();
            authorityArrVO.setId(authorityListVO.getId());
            authorityArrVO.setIndex(authorityListVO.getModuleRoute());
            authorityArrVO.setTitle(authorityListVO.getModuleDescription());
            if(authorityListVO.getModuleId() != null) {
                for (AuthorityArrVO arrVO : authorityArr) {
                    if (arrVO != null && arrVO.getId() != null && arrVO.getId().equals(authorityListVO.getModuleId())) {
                        if (arrVO.getChildren() == null) {
                            arrVO.setChildren(new ArrayList<>());
                        }
                        arrVO.getChildren().add(authorityArrVO);
                    }
                }
            }else {
                authorityArr[count] = authorityArrVO;
                count++;
            }
        }
        Map<String, AuthorityArrVO[]> map = new HashMap<>();
        map.put(name, authorityArr);
        return map;
    }
}
